package com.whxd.framework.security;

import java.util.Iterator;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 类描述
 * 
 * @author 张世鹏
 * @since 2013-8-12
 */
public class SecurityUtils {

	public static FrameworkUserDetails getCurrentSecurityUser() {
		FrameworkUserDetails userDetails = null;
		if (SecurityContextHolder.getContext().getAuthentication() != null) {
//			if ("anonymousUser".equals(SecurityContextHolder.getContext().getAuthentication().getPrincipal())) {
//				return null;
//			}
			userDetails = (FrameworkUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		}
		return userDetails;
	}

	public static Boolean hasRole(String roleKey) {
		Iterator<? extends GrantedAuthority> iterator = SecurityContextHolder.getContext().getAuthentication().getAuthorities().iterator();
		for (Iterator iterator2 = iterator; iterator2.hasNext();) {
			GrantedAuthority type = (GrantedAuthority) iterator2.next();
			if (type.equals(roleKey)) {
				return true;
			}
		}
		return false;
	}
}
